package com.situ.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.ssm.common.ServerResponse;
import com.situ.ssm.dao.UserMapper;
import com.situ.ssm.entity.User;
import com.situ.ssm.service.IUserService;

@Service
public class UserServiceImpl implements IUserService{
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public ServerResponse pageList(Integer page, Integer limit, User user) {
		// 1、使用PageHelper插件设置分页
		PageHelper.startPage(page, limit);
		// 2、查询当前页的所有数据
		List<User> list = userMapper.pageList(user);
		//用PageInfo对结果进行包装
		PageInfo pageInfo = new PageInfo(list);
		Integer count = (int) pageInfo.getTotal();
		
		return ServerResponse.createSuccess("", count, list);
	}

	@Override
	public ServerResponse deleteById(Integer id) {
		int count = userMapper.deleteById(id);
		if (count != 1) {
			return ServerResponse.createError("删除失败");
		}
		
		return ServerResponse.createSuccess("删除成功");
	}

	@Override
	public ServerResponse deleteAll(Integer[] ids) {
		int count = userMapper.deleteAll(ids);
		if (count != ids.length) {
			return ServerResponse.createError("删除失败");
		}
		
		return ServerResponse.createSuccess("删除成功");
	}

	

}
